<?php
namespace app\common\utils;

class SmsHelper
{
    public function __construct()
    {
    }
    /**
     * 发送短信接口
     * @param  [type] $mobile [description]
     * @param  [type] $code   [description]
     * @return [type]         [description]
     * 系统参数
     * SignatureMethod    签名方式。取值范围：HMAC-SHA1。
     * SignatureNonce     签名唯一随机数
     * AccessKeyId        访问秘钥ID
     * SignatureVersion   签名算法版本 1.0
     * Timestamp          请求的时间戳
     * Format             返回的语言类型
     * 
     * 业务API参数
     * Action             请求的API方法名称
     * Version            API 的版本号  2017-05-25
     * RegionId           API支持的RegionID  cn-hangzhou
     *
     * PhoneNumbers       接收短信的手机号码
     * SignName           短信签名名称 
     * TemplateParam      短信模板变量对应的实际值，JSON格式
     * TemplateCode       短信模板ID
     */
    public static function sendCaptchaSms($mobile, $code)
    {
        $Timestamp = gmdate("Y-m-d\TH:i:s\Z");

        #系统参数
        $sysdata = [
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => self::uuid(),
            'AccessKeyId' => env('Aliyun.AccessKey', 'LTAI5tLiEuiYqxK1gRjWbcUf'),
            'SignatureVersion' => "1.0",
            'Timestamp' => $Timestamp,
            'Format' => 'json',
        ];

        #业务API参数
        $apidata = self::apidata($mobile,$code);

        #合并参数
        $datas = array_merge($sysdata,$apidata);

        #升序排列
        ksort($datas);

        #拼接加密字符串
        $sortedQueryStringTmp  = "";
        foreach($datas as $key=>$val)
        {
            $sortedQueryStringTmp  .= "&".$key."=".self::encode($val);
        }
        $stringToSign = "GET&%2F&" . self::encode(substr($sortedQueryStringTmp, 1));

        #加密转码 
        $sign = base64_encode(hash_hmac("sha1", $stringToSign, env('Aliyun.AccessSecret', 'cSGZ48EGKcKB25U0d3lyDZHVvk8ZUJ') . "&",true));

        #生成签名
        $signature = self::encode($sign);

        #拼接请求链接
        $url = "https://dysmsapi.aliyuncs.com/?Signature=".$signature.$sortedQueryStringTmp;

        try {
            $content = self::curlRuqust($url);          
            return json_decode($content,true);
        } catch( \Exception $e) {
            return false;
        }
    }

    /**
     * 业务参数整合
     * @param  [type] $mobile [description]
     * @param  [type] $code   [description]
     * @return [type]         [description]
     */
    public static function apidata($mobile,$code)
    {

        $SignName2 = array();
        $code2 = array();
        foreach ($mobile as $k => $v) {
            $SignName2[] = env('Aliyun.smsCaptchaSignName', 'YXHS');
            $code2[] = "{\"code\":\"".$code[$k]."\"}";
        }

        $apidata = [
            'Action' =>  'SendBatchSms',
            'Version' => '2017-05-25',
            'RegionId' => 'cn-hangzhou',
            'PhoneNumberJson' => json_encode($mobile),
            'SignNameJson' => json_encode($SignName2),
            'TemplateParamJson' => json_encode($code2),
            'TemplateCode' => env('ali.smsCaptchaTemplateCode', 'SMS_183792965'),
        ];
        return $apidata;
    }

    /**
     * 阿里云短信对应转码替换
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    private static function encode($str)
    {
        $res = urlencode($str);
        $res = preg_replace("/\+/", "%20", $res);
        $res = preg_replace("/\*/", "%2A", $res);
        $res = preg_replace("/%7E/", "~", $res);
        return $res;
    }

    /**
     * PHP生成 UUID
     * @return [type] [description]
     */
    public static function uuid()  
    {  
        $chars = md5(uniqid(mt_rand(), true));  
        $uuid = substr ( $chars, 0, 8 ) . '-'
                . substr ( $chars, 8, 4 ) . '-' 
                . substr ( $chars, 12, 4 ) . '-'
                . substr ( $chars, 16, 4 ) . '-'
                . substr ( $chars, 20, 12 );  
        return $uuid ;  
    }  

    /**
     * 对应CURL请求
     * @param  [type] $url [description]
     * @return [type]      [description]
     */
    private static function curlRuqust($url) 
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "x-sdk-client" => "php/2.0.0"
        ));
        if(substr($url, 0,5) == 'https') 
        {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $rtn = curl_exec($ch);
        if($rtn === false) 
        {
            trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
        }
        curl_close($ch); 
        return $rtn;
    }


    /**
     * @author: zwx
     * @Description: 批量发送短信
     * @param {array} $mobile  示例：["15833650367","18131978220"]
     * @param {array} $code    示例：["50367","50367"]
     * @param {*} $path  示例：/index/lk/o/
     * @Date: 2023-11-30 11:31:03
     */        
    public static function smsbatch($mobile, $code, $path='/index/lk/o/')
    {
        $code2 = array();
        foreach ($code as $v) {
            $code2[] = $path.base64_encode($v);
        }
        $res = self::sendCaptchaSms($mobile, $code2);
        return $res;
     
    }



  


}